﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Cal = Caliburn.Micro;

namespace DevProjex.Caliburn.Micro.Extensions
{
    public class CustomPage : Page
    {
        #region Fields

        private Frame _frame;

        #endregion

        #region Constructors

        public CustomPage()
        {
            NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;

            Loaded += CustomPage_Loaded;
        }

        #endregion

        #region Properties, Indexers

        public bool ShowStatusBar
        {
            get { return (bool)GetValue(ShowStatusBarProperty); }
            set { SetValue(ShowStatusBarProperty, value); }
        }

        public SolidColorBrush StatusBarBackgroundColor
        {
            get { return (SolidColorBrush)GetValue(StatusBarBackgroundColorProperty); }
            set { SetValue(StatusBarBackgroundColorProperty, value); }
        }

        public double StatusBarBackgroundOpacity
        {
            get { return (double)GetValue(StatusBarBackgroundOpacityProperty); }
            set { SetValue(StatusBarBackgroundOpacityProperty, value); }
        }

        #endregion

        #region Private Methods

        private void ResetPageCache()
        {
            var cacheSize = _frame.CacheSize;
            _frame.CacheSize = 0;
            _frame.CacheSize = cacheSize;
        }

        #endregion

        #region Static Methods

        private static async void ChangeStatusBar(bool visible)
        {
            var statusBar = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
            if (visible)
                await statusBar.ShowAsync();
            else await statusBar.HideAsync();
        }

        private static void ChangeStatusBarBackgroundColor(SolidColorBrush color)
        {
            if (color == null)
                return;
            var statusBar = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
            statusBar.BackgroundColor = color.Color;
        }

        private static void ChangeStatusBarBackgroundOpacity(double value)
        {
            var statusBar = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
            statusBar.BackgroundOpacity = value;
        }

        private static void ShowStatusBarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (Cal.Execute.InDesignMode)
                return;
#if WINDOWS_PHONE_APP
            ChangeStatusBar((bool)e.NewValue);
#endif
        }

        private static void StatusBarBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (Cal.Execute.InDesignMode)
                return;
#if WINDOWS_PHONE_APP
            ChangeStatusBarBackgroundColor((SolidColorBrush)e.NewValue);
#endif
        }

        private static void StatusBarBackgroundOpacitiyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (Cal.Execute.InDesignMode)
                return;
#if WINDOWS_PHONE_APP
            ChangeStatusBarBackgroundOpacity((double)e.NewValue);
#endif
        }

        #endregion

        #region Virtual, Abstract, Override Methods

        protected override void OnNavigatingFrom(Windows.UI.Xaml.Navigation.NavigatingCancelEventArgs e)
        {
            if (e.NavigationMode == Windows.UI.Xaml.Navigation.NavigationMode.Back)
            {
                ResetPageCache();
                NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Disabled;
            }
        }

        #endregion

        #region Event Handling

        private void CustomPage_Loaded(object sender, RoutedEventArgs e)
        {
            //
            ChangeStatusBar(ShowStatusBar);
            ChangeStatusBarBackgroundColor(StatusBarBackgroundColor);
            ChangeStatusBarBackgroundOpacity(StatusBarBackgroundOpacity);
            //
            if (Cal.Execute.InDesignMode)
                return;
            //
            _frame = ((Frame)Parent);
        }

        #endregion

        public static readonly DependencyProperty StatusBarBackgroundColorProperty = DependencyProperty.Register(
            "StatusBarBackgroundColor", typeof(SolidColorBrush), typeof(CustomPage), new PropertyMetadata(default(SolidColorBrush), StatusBarBackgroundColorChanged));

        public static readonly DependencyProperty StatusBarBackgroundOpacityProperty = DependencyProperty.Register(
            "StatusBarBackgroundOpacity", typeof(double), typeof(CustomPage), new PropertyMetadata(1.0, StatusBarBackgroundOpacitiyChanged));

        public static readonly DependencyProperty ShowStatusBarProperty = DependencyProperty.Register(
            "ShowStatusBar", typeof(bool), typeof(CustomPage), new PropertyMetadata(default(bool), ShowStatusBarChanged));
    }
}
